package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.services.AccountService;
import org.darkforest.common.services.OrderService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.model.Member;
import org.darkforest.model.Order;
import org.darkforest.model.OrderVo;
import org.darkforest.model.RestEntity;
import org.darkforest.ucenter.service.UserOrderService;
import org.darkforest.vo.MemberBase;
import org.darkforest.vo.OrderCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单控制层
 *
 * @auth bingo
 * @create 2017-10-10-19:04
 */
@RestController
@RequestMapping(value = "order")
@Api(value = "order", description = "订单",produces= MediaType.APPLICATION_JSON_VALUE)
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserOrderService userOrderService;

    /**
     * 订单列表 目前只提供订单号 以及 订单状态查询
     *
     * @param member
     * @param state
     * @param orderSn
     * @param start
     * @param pageSize
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ApiOperation(value = "订单列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity list(@CurrentMember Member member,
                           @RequestParam(required = false, defaultValue = "") String state,
                           @RequestParam(required = false, defaultValue = "") String orderSn,
                           @RequestParam(required = false, defaultValue = "0") Integer start,
                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        try{
            Order param = new Order();
            param.setBuyerId(member.getMemberId());
            param.setPageSize(pageSize);
            param.setStart(start);
            param.setOrderSn(StringUtils.isBlank(orderSn)? null : Long.valueOf(orderSn));
            param.setOrderState(StringUtils.isBlank(state) ? null : state);
            List<Order> orderList = orderService.findOrderWithGoodsByCondition(param);
            return RestEntity.success(orderList);
        }catch (Exception e){
            e.printStackTrace();
            return  RestEntity.failed("获取异常");
        }
    }

    @AuthPassport
    @RequestMapping(value = "info",method = RequestMethod.GET)
    @ApiOperation(value = "订单详情", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity info(@CurrentMember Member member,  @RequestParam(required = true) Integer orderId) {
        try{
            OrderVo vo = userOrderService.getOrderDetailById(orderId, member.getMemberId());
            return RestEntity.success(vo);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed("获取异常");
        }
    }

    @AuthPassport
    @RequestMapping(value = "cancel",method = RequestMethod.GET)
    @ApiOperation(value = "取消订单", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity cancel(@CurrentMember Member member, @RequestParam(required = true) Integer orderId){
        try {
            MemberBase memberBase = new MemberBase();
            memberBase.setUserId(member.getMemberId());
            memberBase.setEmail(member.getMemberEmail());
            memberBase.setUserName(member.getMemberName());
            memberBase.setTrueName(member.getMemberName());
            Order order = orderService.selectById(orderId);
            orderService.doCancelOrder(order,memberBase);
            return RestEntity.success();
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed("取消异常");
        }
    }

    /**
     * 订单个数查询
     * @param member
     * @param state
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "listSize",method = RequestMethod.GET)
    @ApiOperation(value = "订单个数", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity listSize(@CurrentMember Member member,
                           @RequestParam(required = false, defaultValue = "") String state) {
        try{
            Order param = new Order();
            param.setBuyerId(member.getMemberId());
            param.setPageSwitch(false);
            param.setOrderState(StringUtils.isBlank(state) ? null : state);
            List<Order> orderList = orderService.findOrderWithGoodsByCondition(param);
            return RestEntity.success(orderList.size());
        }catch (Exception e){
            e.printStackTrace();
            return  RestEntity.failed("获取异常");
        }
    }


    @RequestMapping(value = "sureReceiveGoods")
    @AuthPassport
    @ApiOperation(value = "确认收货", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity sureReceiveGoods(@CurrentMember Member member,Integer orderId){
        Order order = orderService.selectOrderByPrimaryKey(orderId);
        if(order != null){
            if("30".equals(order.getOrderState()) && order.getBuyerId() .equals(member.getMemberId())){
                order.setOrderState("40");
                order.setFinnshedTime(DateUtil.unixTimeStampInt().intValue());
                orderService.updateByPrimaryKeySelective(order);
                return RestEntity.success();
            }else{
                return RestEntity.failed("操作失败，请勿重复确认收货");
            }
        }else{
            return RestEntity.failed("无效订单");

        }

    }

}
